<template>
  <el-dialog :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible" :close-on-click-modal="false"
    draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item prop="uid" :label="$t('UserFeedback.uid')">
            <el-input v-model="form.uid" :placeholder="$t('UserFeedback.inputUidTip')" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="type" :label="$t('UserFeedback.type')">
            <el-select v-model="form.type" :placeholder="$t('UserFeedback.inputTypeTip')" clearable filterable>
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_user_feedback_type"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="email" :label="$t('UserFeedback.email')">
            <el-input v-model="form.email" :placeholder="$t('UserFeedback.inputEmailTip')" clearable show-word-limit
              :maxlength="100" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="title" :label="$t('UserFeedback.title')">
            <el-input v-model="form.title" :placeholder="$t('UserFeedback.inputTitleTip')" clearable show-word-limit
              :maxlength="255" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="content" :label="$t('UserFeedback.content')">
            <el-input v-model="form.content" :placeholder="$t('UserFeedback.inputContentTip')" clearable show-word-limit
              :maxlength="1024" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="pics" :label="$t('UserFeedback.pics')">
            <upload-img v-model:image-url="form.pics"></upload-img>
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item prop="source" :label="$t('UserFeedback.source')">
            <el-select v-model="form.source" :placeholder="$t('UserFeedback.inputSourceTip')" clearable filterable>
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in biz_source"
                :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="UserFeedbackDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/UserFeedback'
const emit = defineEmits(['refresh']);

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 定义字典
import { useDict } from '/@/hooks/dict';
const { biz_user_feedback_type, biz_source } = useDict('biz_user_feedback_type', 'biz_source')

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
  id: '',
  uid: '',
  type: '',
  email: '',
  title: '',
  content: '',
  pics: '',
  source: '',
});

// 定义校验规则
import { rule } from '/@/utils/validate';
const dataRules = ref({
  uid: [{ required: true, message: t('UserFeedback.inputUidTip'), trigger: 'blur' }],
  type: [{ required: true, message: t('UserFeedback.inputTypeTip'), trigger: 'blur' }],
  email: [{ required: false, message: t('UserFeedback.inputEmailTip'), trigger: 'blur' }, { validator: rule.email, trigger: 'blur' }],
  title: [{ required: true, message: t('UserFeedback.inputTitleTip'), trigger: 'blur' }],
  content: [{ required: true, message: t('UserFeedback.inputContentTip'), trigger: 'blur' }],
  pics: [{ required: false, message: t('UserFeedback.inputPicsTip'), trigger: 'blur' }],
  source: [{ required: false, message: t('UserFeedback.inputSourceTip'), trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 暴露变量
defineExpose({
  openDialog
});
</script>
